﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Web.Script.Serialization;
using Fuse8.DomainFramework.BusinessService;

namespace Fuse8.ContentManagementFramework.Domain
{
    public partial class CmsTemplateRegion
    {
        private IEnumerable<CmsPageControl> _regionControls;

        [XmlIgnore]
        [ScriptIgnore]
        public IEnumerable<CmsPageControl> RegionControls
        {
            get
            {
                // note it is necessary to call ToList() to safely put _regionControls to cache or state server session
                if (_regionControls == null)
                {
                    var businessFacade = new BusinessServiceFacade<CmsPageControl, int>();                    
                    _regionControls = businessFacade.Find(p => p.TemplateRegionId == Id).ToList();
                    
                }

                return _regionControls;
            }
            set
            {
                if (value != null)
                {
                    _regionControls = value.ToList();
                }
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public bool HasControls
        {
            get
            {
                return _regionControls != null;
            }
        }
    }
}
